package com.geek.bams.client;

import com.geek.bams.client.BusinessPanel;
import com.geek.bams.client.LoginPanel;
import com.geek.bams.client.MainPanel;
import com.geek.bams.client.RegisterPanel;
import com.geek.bams.entity.Account;

import javax.swing.*;
import java.awt.*;

/**
 * @Desc ATM客户端主类
 * @Author lirui
 * @Date 2025/10/22
 */
public class ATMClient extends JFrame {

    MainPanel mainPanel;
    RegisterPanel registerPanel;
    LoginPanel loginPanel;
    BusinessPanel businessPanel;

    //共同资源
    ClientService clientService; // 网络通信服务
    Account acc;
    String currentPassword; // 保存当前登录用户的密码，用于后续操作

    public ATMClient() {
        //设置窗体的基本信息
        this.setTitle("BAMS Client");
        this.setSize(500 , 650);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setResizable(false);

        //初始化网络通信服务
        clientService = new ClientService();
        // 尝试连接服务器
        if (!clientService.connect()) {
            JOptionPane.showMessageDialog(this, 
                "无法连接到服务器！\n请确保服务器已启动。", 
                "连接失败", 
                JOptionPane.WARNING_MESSAGE);
        }

        //实例化JPanel
        mainPanel = new MainPanel(this);
        registerPanel = new RegisterPanel(this);
        loginPanel = new LoginPanel(this);
        businessPanel = new BusinessPanel(this);

        //设置窗体的布局方式为"卡片式布局"
        Container container = this.getContentPane();
        container.setLayout(new CardLayout());
        container.add("mainPanel", mainPanel);
        container.add("registerPanel", registerPanel);
        container.add("loginPanel", loginPanel);
        container.add("businessPanel", businessPanel);
        
        // 窗口关闭时断开连接
        this.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                clientService.disconnect();
            }
        });
    }

    public static void main(String[] args) {
        new ATMClient().setVisible(true);
    }
}